package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.jzapi.utils.HttpClientUtil;
import com.jinzhi.jzweb.domain.CertificationBatchDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.CertificatesExportVo;
import com.jinzhi.jzweb.pojo.CertificatesVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.service.TrackDrawService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2023-10-16 16:36:18 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trackDraw")
public class TrackDrawController extends BaseController {
	@Autowired
	private TrackDrawService trackDrawService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:trackDraw:trackDraw")
	String TrackDraw(){
	    return "jzweb/trackDraw/trackDraw";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:trackDraw:trackDraw")
	public Result<List<TrackDrawDO>> list(TrackDrawDO trackDrawDTO){

		return trackDrawService.selectDraw(trackDrawDTO);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:trackDraw:add")
	String add(){
	    return "jzweb/trackDraw/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trackDraw:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TrackDrawDO trackDraw = trackDrawService.selectById(id);
		model.addAttribute("trackDraw", trackDraw);
	    return "jzweb/trackDraw/edit";
	}
	
	@Log("添加赛道抽签表（存储抽签信息）")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trackDraw:add")
	public Result<String> save( TrackDrawDO trackDraw){
		trackDrawService.insert(trackDraw);
        return Result.ok();
	}
	
	@Log("修改赛道抽签表（存储抽签信息）")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trackDraw:edit")
	public Result<String>  update( TrackDrawDO trackDraw){
		boolean update = trackDrawService.updateById(trackDraw);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除赛道抽签表（存储抽签信息）")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trackDraw:remove")
	public Result<String>  remove( Long id){
		trackDrawService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除赛道抽签表（存储抽签信息）")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trackDraw:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		trackDrawService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	/**
	 * 导出抽签结果信息
	 *
	 * @param response
	 */
	@Log("导出抽签结果信息")
	@RequestMapping("/downloadImportTemplate")
	@RequiresPermissions("jzweb:trackDraw:import")
	@ResponseBody
	public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response, TrackDrawDO trackDrawDTO) {
		Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
		wrapper.orderBy(true,"sort");
		List<TrackDrawDO> trackDrawDOS = trackDrawService.selectList(wrapper);
		if (trackDrawDOS != null && trackDrawDOS.size() > 0) {
			//导出操作
			EasyPoiUtil.exportExcel(trackDrawDOS, "江西赣州市婴幼儿保育赛道抽签信息", "江西赣州市婴幼儿保育赛道抽签信息", TrackDrawDO.class,  "江西赣州市婴幼儿保育赛道抽签信息", response);
		} else {
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
	}

	/**
	 * 查询上次的抽签结果
	 */
	@ResponseBody
	@GetMapping("/trackList")
	@RequiresPermissions("jzweb:trackDraw:trackDraw")
	public Result<TrackDrawDO> trackList(TrackDrawDO trackDrawDTO){

		return Result.ok(trackDrawService.trackList(trackDrawDTO));
	}


}
